package com.pacy.pacybaseproject.constviews;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.pacy.pacybaseproject.R;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.views.bottombar.BottomBarTab;
import me.goldze.mvvmhabit.views.bottombar.BottomCheckBar;

/**
 * @author PACY
 * @date 2024/4/7
 * description:
 */
public class MyBottomBar extends LinearLayout {
    private List<BottomBarTab> mTabs = new ArrayList<>();
    private LinearLayout lin1,lin2;
    private static final int TRANSLATE_DURATION_MILLIS = 200;

    private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator();
    private boolean mVisible = true;

    private LayoutParams mTabParams;
    private int mCurrentPosition = 0;
    private BottomCheckBar.OnTabSelectedListener mListener;

    private FragmentManager manager;
    private int fragmentId;//父控件id
    private List<Fragment> mFragments = new ArrayList<>();//fragment
    public MyBottomBar(Context context) {
        super(context);
    }

    public MyBottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.my_bottom_bar, this, true);
        mTabParams = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
        mTabParams.weight = 1;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        lin1=findViewById(R.id.lin1);
        lin2=findViewById(R.id.lin2);
    }

    /**
     * 初始化
     *
     * @param manager
     * @param fragmentId
     * @return
     */
    public MyBottomBar init(FragmentManager manager, int fragmentId) {
        this.manager = manager;
        this.fragmentId = fragmentId;
        return this;
    }

    public MyBottomBar setFragments(List<Fragment> fragments) {
        this.mFragments = fragments;
        return this;
    }

    public MyBottomBar addItems( List<BottomBarTab> tabs) {
        mTabs=tabs;
        for (int i = 0; i < mTabs.size(); i++) {
            int finalI = i;
            mTabs.get(i).setOnClickListener(v -> {
                if (mListener == null) return;

                int pos = mTabs.get(finalI).getTabPosition();
                if (mCurrentPosition == pos) {
                    mListener.onTabReselected(pos);
                } else {
                    mListener.onTabSelected(pos, mCurrentPosition);
                    mTabs.get(finalI).setSelected(true);
                    mListener.onTabUnselected(mCurrentPosition);
                    mTabs.get(mCurrentPosition).setSelected(false);
                    mCurrentPosition = pos;
//                    setCurrentFragment(mCurrentPosition);
                }
            });
            mTabs.get(i).setTabPosition(i);
            mTabs.get(i).setLayoutParams(mTabParams);
            if(i<2){
                lin1.addView(mTabs.get(i));
            }else{
                lin2.addView(mTabs.get(i));
            }
        }
        return this;
    }


    /**
     * 设置选中
     *
     * @param index
     */
    public void setCurrentFragment(int index) {
        if (manager == null) return;
        FragmentTransaction ft = manager.beginTransaction();
        hideAllFragment(ft);
        Fragment currentFragment = mFragments.get(index);
        hideAllFragment(ft);
        if (!currentFragment.isAdded()) {
            ft.add(fragmentId, currentFragment);
        }
        ft.show(currentFragment);
        ft.commitAllowingStateLoss();

    }

    /**
     * 隐藏所有Fragment
     *
     * @param fragmentTransaction
     */
    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (fragmentTransaction != null) {
            for (int i = 0; i < mFragments.size(); i++) {
                fragmentTransaction.hide(mFragments.get(i));
            }
        }
    }
}
